package com.alibaba.org.controller;

import com.alibaba.basic.dto.ShopDto;
import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.ExcelUtil;
import com.alibaba.basic.util.PageList;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.domain.ShopAuditLog;
import com.alibaba.org.query.ShopQuery;
import com.alibaba.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 接口类：店铺
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口",description = "店铺的CRUD功能") // Swagger注解：对Controller做描述
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口：查询单个对象
     * http://localhost:8080/shop/1
     * @param id
     * @return
     */
    @GetMapping("/{id}") // 带参数的get请求
    @ApiOperation(value = "查询单个对象")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     * http://localhost:8080/shop
     * @return
     */
    @GetMapping // 不带参数的get请求
    @ApiOperation(value = "查询所有对象")
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    /**
     * 接口：分页查询 + 高级查询
     * http://localhost:8080/shop + {json对象}
     * @RequestBody: 从请求体【请求数据包中】中获取数据
     * @param shopQuery
     * @return
     */
    @PostMapping // 发送post请求，从请求体【请求数据包中】中获取分页查询 + 高级查询数据
    @ApiOperation(value = "分页查询")
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery) {
        return shopService.queryPage(shopQuery);
    }

    /**
     * 接口：删除
     * http://localhost:8080/shop/1
     * @return
     */
    @DeleteMapping("/{id}") // 发送删除请求
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：批量删除
     * http://localhost:8080/shop/1
     * @return
     */
    @PatchMapping() // 发送删除请求
    @ApiOperation(value = "批量删除")
    public AjaxResult batchDelete(@RequestBody Long[] ids) {
        try {
            shopService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * http://localhost:8080/department + {json对象}
     * @RequestBody: 从请求体【请求数据包中】中获取数据
     * @param shop
     * @return
     */
    @PutMapping // 可以用于添加或修改资源
    @ApiOperation(value = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null) { // 修改数据
                shopService.update(shop);
            } else { // 添加数据
                shopService.add(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 接口：店铺入住
     * http://localhost:8080/shop/1
     * @return
     */
    @PostMapping("settlement") // 发送删除请求
    @ApiOperation(value = "店铺入住")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false, "入驻失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试。忘记密码，请联系管理员！！");
        }
    }

    /**
     * 接口：店铺审核成功
     * http://localhost:8080/shop/1
     * @return
     */
    @PostMapping("/audit/pass") // 发送删除请求
    @ApiOperation(value = "店铺审核成功")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "审核失败");
        }
    }

    /**
     * 接口：店铺驳回成功
     * http://localhost:8080/shop/1
     * @return
     */
    @PostMapping("/audit/reject") // 发送删除请求
    @ApiOperation(value = "店铺驳回成功")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "驳回失败");
        }
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file){

        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    @GetMapping("/echarts")
    public List<ShopDto> getShopState(){
        return shopService.getShopState();
    }

    /**
     * 店铺邮件激活
     * @param id
     * @param response
     */
    @GetMapping("/active/{id}")
    public void shopActive(@PathVariable ("id") Long id, HttpServletResponse response){
        try {
            shopService.shopActive(id);
            response.sendRedirect("http://localhost:8081/#/login");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
